#!/usr/bin/python
# -*- coding: UTF-8 -*-


import time,threading,sys
sys.path.append("/home/biowin/Minibot/04.RobotSamples/03.SpiderRobot")
# sys.path.append("../../")
from BwRobotLib import BwRobotLib
from SpiderRobot import SpiderRobot
from BwSensorLib import BwSensorLib
import os,inspect

# 加载机器人构型配置
fileName = inspect.getframeinfo(inspect.currentframe()).filename
folderPath     = os.path.dirname(os.path.abspath(fileName))
cfile=folderPath+"/config.txt"


class RemoteRobot:
	def __init__(self):
		# 实例化传感器库对象
		self.sensorlib = BwsensorliblibLib("udp")
		# 实例化BwRobotLib库
		self.robotlib = BwRobotLib("udp")
		# 实例化蜘蛛机器人库
		self.spider = SpiderRobot(self.robotlib)
		# 连接机器人设备
		self.robotlib.connectRobot("10.10.100.254",8899) #真机AP模式
        # 导入机器人配置参数
        self.robotlib.loadConfig(config_file)
		#配置ID
		self.spider.bindIds(self.robotlib.module_ids)
		# 点阵显示
		# 点阵图案：向上箭头 向下箭头　向左箭头　向右箭头 　惊喜  右眨眼
		self.display_data = [[0x08,0x1C,0x2A,0x08,0x08],[0x08,0x08,0x2A,0x1C,0x08],[0x08,0x10,0x3F,0x10,0x08],
							[0x08,0x04,0x7E,0x04,0x08]，[0x22,0x77,0x22,0x1C,0x14]，[0x02,0x24,0x02,0x08,0x1C]]		
        # RGB彩灯参数
        # [255,1,1] 红色；[1,255,1] 绿色；[1,1,255] 蓝色；[255,255,1] 黄色; [255,1,255] 紫色 [1,255,255] 青色；
        self.rgb_value = [[255, 1, 1], [1, 255, 1], [1, 1, 255], [255,255,1],[255,1,255],[1,255,255]]

	
	def remoteRobotMotion(self):
		while 1:
			print("请按下按键：")
			remote= self.sensor.readRemoteSensor() # 读取遥控传感器数据			
			print ("返回值：",remote)
			# 按下↑键
			if(remote==2):
				print("按下＂↑＂－－向前走")
				self.sensolib.setMatrixSensor(self.display_data[0]) # 点阵显示向上箭头
				self.sensorlib.setRGBLightSensor(self.rgb_value[0]) # 亮红灯
				self.spider.goForward() # 蜘蛛机器人向前走
			# 按下↓键
			if(remote==8):
				print("按下＂↓＂－－向后走")
				self.sensorlib.setMatrixSensor(self.display_data[1]) # 点阵显示向下箭头
				self.sensorlib.setRGBLightSensor(self.rgb_value[1]) # 亮绿灯
				self.spider.goBack() # 蜘蛛机器人向后走
			# 按下←键
			if(remote==4):
				print("按下＂←＂－－向左转向")
				self.sensorlib.setMatrixSensor(self.display_data[2]) # 点阵显示向左箭头
				self.sensorlib.setRGBLightSensor(self.rgb_value[2]) # 亮蓝灯
				self.spider.turnLeft() # 蜘蛛机器人向左转
			# 按下→键
			if(remote==6):
				print("按下＂→＂－－向右转向")
				self.sensorlib.setMatrixSensor(self.display_data[3]) # 点阵显示向上右箭头
				self.sensorlib.setRGBLightSensor(self.rgb_value[3]) # 亮黄灯
				self.spider.turnRight() # 蜘蛛机器人向右转
			# 按下1键
			if(remote==10):
				print("按下＂1＂－－跳舞")
				self.sensorlib.setMatrixSensor(self.display_data[4]) # 点阵显示惊喜表情
				self.sensorlib.setRGBLightSensor(self.rgb_value[4]) # 亮紫灯
				self.spider.dance() # 蜘蛛机器人跳舞
			# 按下2键
			if(remote==11):
				print("按下＂2＂－－俯卧撑")
				self.sensorlib.setMatrixSensor(self.display_data[5]) # 点阵显示右眨眼表情
				self.sensorlib.setRGBLightSensor(self.rgb_value[5]) # 亮青灯
				self.spider.doSport() # 蜘蛛机器人做俯卧撑


# 主函数			
def main():
    # 实例化遥控控制类
    remoteRobot = RemoteRobot()
    # 执行遥控控制功能
    remoteRobot.remoteRobotMotion()


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print(" Program closed! ")
    pass

